package com.pda.xinma.ui.manmade;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.google.gson.Gson;
import com.pda.xinma.R;
import com.pda.xinma.adapter.EndFaceAdapter;
import com.pda.xinma.adapter.EndFaceNewAdapter;
import com.pda.xinma.bean.ArtificialListBean;
import com.pda.xinma.bean.EndFaceListBean;
import com.pda.xinma.bean.ScanBean;
import com.pda.xinma.bean.SelectBean;
import com.pda.xinma.bean.WorkSurfaceBean;
import com.pda.xinma.contract.ManMadeContract;
import com.pda.xinma.presenter.ManMadePresenter;
import com.pda.xinma.utils.PopUtisl;
import com.pda.xinma.utils.UserUtils;
import com.pda.xinma.view.ScanButView;
import com.pda.xinma.view.datepicker.CustomDatePicker;
import com.pda.xinma.view.datepicker.DateFormatUtils;
import com.wangshen.base.app.AppActivityKey;
import com.wangshen.base.config.StringKey;
import com.wangshen.base.ui.mvp.base.ui.BaseMvpFragment;
import com.wangshen.base.util.SharedPreferencesUtil;
import com.wangshen.base.util.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

import static com.pda.xinma.ui.xinruku.InStoregeActivity.getYearDateTime;

public class EndFaceFragment extends BaseMvpFragment<ManMadePresenter> implements ManMadeContract.View {
    @BindView(R.id.tv_center)
    TextView tvCenter;
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.sb_scan)
    ScanButView sbScan;
    @BindView(R.id.et_search)
    EditText etSearch;
    @BindView(R.id.tv_search)
    TextView tvSearch;
    @BindView(R.id.tv_indexNo)
    TextView tvIndexNo;
    @BindView(R.id.tv_startTime)
    TextView tvStartTime;
    @BindView(R.id.tv_endTime)
    TextView tvEndTime;
    private String storge;
    EndFaceNewAdapter endFaceNewAdapter;
    public EndFaceFragment() {
    }

    public EndFaceFragment(String storge) {
        this.storge = storge;
    }

    @Override
    public ManMadePresenter getPresenter() {
        return new ManMadePresenter();
    }

    @Override
    public int getViewId() {
        return R.layout.fragment_end_face;
    }

    @Override
    public void initView() {
        endFaceNewAdapter=new EndFaceNewAdapter(R.layout.item_end_face_new,new ArrayList<>());
        rvList.setAdapter(endFaceNewAdapter);
        initDatePicker();
        if (TextUtils.isEmpty(SharedPreferencesUtil.getInstance().getString(UserUtils.getInstance().getUserBean().getUserName()+"center",""))){
            presenter.getCenterByUserId();
        }else {
            String center=SharedPreferencesUtil.getInstance().getString(UserUtils.getInstance().getUserBean().getUserName()+"center","");
            faceBean=new Gson().fromJson(center,SelectBean.class);
            tvCenter.setText(faceBean.getName());
            endFaceNewAdapter.setId(faceBean.getId());
        }
    }

    @Override
    public void initLinsenterner() {


    }

    @Override
    public void onResume() {
        super.onResume();
        sbScan.setScanCallBack(new ScanButView.ScanCallBack() {
            @Override
            public void callBack(ScanBean scanBean) {
                if (scanBean.getCode().contains("order")) {
                    int lenth = scanBean.getCode().indexOf("A");
                    String order = scanBean.getCode().substring(lenth + 1);
                    if (faceBean != null) {
                        getList(order);
                        //presenter.selectScanArtificialDeliveryList(storge, faceBean.getId(), order);
                    } else {
                        ToastUtils.showToast(mContext, "请先选择工作中心！");
                    }

                }
            }
        });
        if (faceBean != null) {
            getList("");
            //presenter.selectArtificialDeliveryList(storge, faceBean.getId());
        }
    }
    private void getList(String order){
        presenter.selectArtificialList(storge,faceBean.getId(),order,tvStartTime.getText().toString(),tvEndTime.getText().toString());
    }
    @Override
    public void onPause() {
        super.onPause();
        sbScan.setScanCallBack(null);
    }

    @Override
    public void getData() {

    }

    SelectBean faceBean;

    @Override
    public void setFaceList(List<WorkSurfaceBean> list) {
        if (list.size() > 0 && TextUtils.isEmpty(tvCenter.getText())) {
            SelectBean selectBean = new SelectBean(list.get(0).getCenterName(), list.get(0).getCenterCode());
            faceBean = selectBean;
            tvCenter.setText(selectBean.getName());
            SharedPreferencesUtil.getInstance().putString(UserUtils.getInstance().getUserBean().getUserName()+"center",new Gson().toJson(faceBean));
            endFaceNewAdapter.setId(faceBean.getId());
            getList("");
            //presenter.selectArtificialDeliveryList(storge, faceBean.getId());
            return;
        }
        List<SelectBean> selectBeans = new ArrayList<>();
        for (WorkSurfaceBean bean : list) {
            selectBeans.add(new SelectBean(bean.getCenterName(), bean.getCenterCode()));
        }
        PopUtisl.showSelectPop(mContext, selectBeans, tvCenter, new PopUtisl.SelectClick() {
            @Override
            public void onClick(SelectBean bean) {
                faceBean = bean;
                tvCenter.setText(bean.getName());
                SharedPreferencesUtil.getInstance().putString(UserUtils.getInstance().getUserBean().getUserName()+"center",new Gson().toJson(faceBean));
                endFaceNewAdapter.setId(faceBean.getId());
                getList("");
                //presenter.selectArtificialDeliveryList(storge, faceBean.getId());
            }
        }, "请选择工作中心");
    }
    private CustomDatePicker mDatePicker;
    private void initDatePicker() {
        long beginTimestamp = DateFormatUtils.str2Long("2000-01-01", false);
        long endTimestamp = DateFormatUtils.str2Long("2500-01-01", false);
        // 通过时间戳初始化日期，毫秒级别
        mDatePicker = new CustomDatePicker(mContext, new CustomDatePicker.Callback() {
            @Override
            public void onTimeSelected(long timestamp, TextView v) {
                v.setText(getYearDateTime(timestamp));
                getList("");
            }
        }, beginTimestamp, endTimestamp);
    }
    @Override
    public void setList(List<EndFaceListBean> list) {
        //endFaceAdapter.setNewInstance(list);
    }

    @Override
    public void setNewList(List<ArtificialListBean> list) {
        if (list== null || list.size() == 0){
            showMessage("未查询到数据！");
            endFaceNewAdapter.setNewInstance(new ArrayList<>());
            tvIndexNo.setText("0");
        }else {
            endFaceNewAdapter.setNewInstance(list);
            tvIndexNo.setText(list.size()+"");
        }

    }

    @OnClick(R.id.tv_center)
    public void onViewClicked() {
        presenter.getCenterByUserId();
    }

    @OnClick(R.id.tv_search)
    public void onViewClicked1() {
        // 获取InputMethodManager服务的引用
        InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
// 关闭软键盘
        if (imm != null) {
            imm.hideSoftInputFromWindow(getView().getWindowToken(), 0);
        }
        if (TextUtils.isEmpty(etSearch.getText())) {
            ToastUtils.showToast(mContext, "搜素内容不能为空！");
            return;
        }
        if (faceBean != null) {
            getList(etSearch.getText().toString());
           // presenter.selectSearchArtificialDeliveryList(storge, faceBean.getId(), etSearch.getText().toString());
        } else {
            ToastUtils.showToast(mContext, "请先选择工作中心！");
        }
    }

    @OnClick({R.id.tv_startTime, R.id.tv_endTime})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_startTime:
                // 通过时间戳初始化日期，毫秒级别
                mDatePicker.show(System.currentTimeMillis(), tvStartTime);
                break;
            case R.id.tv_endTime:
                // 通过时间戳初始化日期，毫秒级别
                mDatePicker.show(System.currentTimeMillis(), tvEndTime);
                break;
        }
    }
}
